package com.tangkun.environment.network;


import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.RequestBody;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.entity.gongzuo.CategoryTreeDataReturn;
import com.tangkun.environment.entity.gongzuo.DepartmentAreaReturn;
import com.tangkun.environment.entity.gongzuo.DepartmentChildReturn;
import com.tangkun.environment.entity.gongzuo.EnterpriseDetailReturn;
import com.tangkun.environment.entity.gongzuo.EnterpriseQueryReturn;
import com.tangkun.environment.entity.gongzuo.EnterpriseReturn;
import com.tangkun.environment.entity.gongzuo.InspectViewReturn;
import com.tangkun.environment.entity.gongzuo.ReportIndexReturn;
import com.tangkun.environment.entity.gongzuo.ReportViewReturn;
import com.tangkun.environment.entity.gongzuo.WorkIndexReturn;
import com.tangkun.environment.entity.login.DepartmentscopeIndexReturn;
import com.tangkun.environment.entity.login.UpDateInfo;
import com.tangkun.environment.entity.login.VerifyReturn;
import com.tangkun.environment.entity.personcenter.BindReturn;
import com.tangkun.environment.entity.personcenter.NoticeReturn;
import com.tangkun.environment.entity.personcenter.UploadResourceReturn;
import com.tangkun.environment.entity.shouye.EnterpriseViewReturn;
import com.tangkun.environment.entity.shouye.ErrorListReturn;
import com.tangkun.environment.entity.shouye.IndexIndexReturn;
import com.tangkun.environment.entity.shouye.InspectflowIndexReturn;
import com.tangkun.environment.entity.shouye.NewsViewReturn;
import com.tangkun.environment.entity.token.LoginReturn;
import com.tangkun.environment.entity.xinwen.MarqueetReturn;
import com.tangkun.environment.entity.xinwen.NewsReturn;
import com.tangkun.environment.entity.xinwen.QuestionIndexReturn;
import com.tangkun.environment.entity.xinwen.QuestionReturn;
import com.tangkun.environment.entity.xinwen.TypeReturn;
import com.tangkun.environment.entity.xinwen.ZiXunDetailReturn;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import retrofit.http.Field;
import retrofit.http.FieldMap;
import retrofit.http.FormUrlEncoded;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.PartMap;
import retrofit.http.Path;
import rx.Observable;

/**
 * Created Time: 2017/3/18.
 * <p/>
 * Author:tangkun
 * <p/>
 * 功能：网络请求工具及其接口
 */
public class NetworkUtil {
    //总请求路径
    public static final String API_URL = GlobalUrl.SERVICE_URL;
    private static Retrofit sRetrofit;

    //定义接口
    public interface FromNetwork {


        /**
         * @param type   1，注册；2，找回密码
         * @param mobile 手机号
         */
        //获取验证码
        @FormUrlEncoded
        @POST("/jf/api/user/getValicode")
        Observable<VerifyReturn> getValicode(@Field("type") int type, @Field("mobile") String mobile);

        //图片上传处理  头像
        @POST("/jf/api/image/user")
        @Multipart
        Observable<UploadResourceReturn> uploadImageUser(@PartMap Map<String, RequestBody> params);

        //图片上传处理  企业logo
        @POST("/jf/api/image/ep-logo")
        @Multipart
        Observable<UploadResourceReturn> uploadImageEplogo(@PartMap Map<String, RequestBody> params);

        //图片上传处理  举报企业图片
        @POST("/jf/api/image/report")
        @Multipart
        Observable<UploadResourceReturn> uploadImageReport(@PartMap Map<String, RequestBody> params);

        //图片上传处理  关联企业 营业执照
        @POST("/jf/api/image/ep-license")
        @Multipart
        Observable<UploadResourceReturn> uploadImageEplicense(@PartMap Map<String, RequestBody> params);

        //图片上传处理  关联企业 房产证
        @POST("/jf/api/image/ep-fangchan")
        @Multipart
        Observable<UploadResourceReturn> uploadImageEpfangchan(@PartMap Map<String, RequestBody> params);

        //图片上传处理  关联企业 法人身份证
        @POST("/jf/api/image/ep-legal")
        @Multipart
        Observable<UploadResourceReturn> uploadImageEplegal(@PartMap Map<String, RequestBody> params);

        //图片上传处理  关联企业 环境照片
        @POST("/jf/api/image/ep-environ")
        @Multipart
        Observable<UploadResourceReturn> uploadImageEpenviron(@PartMap Map<String, RequestBody> params);

        //图片上传处理  咨询图片
        @POST("/jf/api/image/question")
        @Multipart
        Observable<UploadResourceReturn> uploadImageQuestion(@PartMap Map<String, RequestBody> params);

        //图片上传处理  巡查数据
        @POST("/jf/api/image/inspect")
        @Multipart
        Observable<UploadResourceReturn> uploadImageInspect(@PartMap Map<String, RequestBody> params);

        //更改用户信息
        @FormUrlEncoded
        @POST("/jf/api/user/update")
        Observable<LoginReturn> updateUser(@FieldMap Map<String, String> map);

        //用户注册
        @FormUrlEncoded
        @POST("/jf/api/user/register")
        Observable<LoginReturn> register(@Field("username") String username, @Field("password") String password, @Field("sex") String sex, @Field("nickname") String nickname, @Field("img") String img);

        //登陆验证
        @FormUrlEncoded
        @POST("/jf/api/login/vali")
        Observable<LoginReturn> login(@Field("username") String username, @Field("password") String password);

        //注销登陆
        @POST("/jf/api/login/logout")
        Observable<CommonReturn> loginout();

        //数据字典查询
        @FormUrlEncoded
        @POST("/jf/api/dict/index")
        Observable<TypeReturn> queryType(@Field("numbers") String numbers);

        //首页广告轮播图
        @POST("/jf/api/marquee/index")
        Observable<MarqueetReturn> queryIndexMarquee();

        //新闻列表
        @FormUrlEncoded
        @POST("/jf/api/news/index")
        Observable<NewsReturn> queryIndexNews(@FieldMap Map<String, String> map);

        //咨询列表
        @FormUrlEncoded
        @POST("/jf/api/question/query")
        Observable<QuestionReturn> queryQuestion(@FieldMap Map<String, String> map);

        //咨询列表 我提交的
        @FormUrlEncoded
        @POST("/jf/api/question/index")
        Observable<QuestionIndexReturn> queryIndexQuestion(@FieldMap Map<String, String> map);

        //创建咨询
        @FormUrlEncoded
        @POST("/jf/api/question/save")
        Observable<CommonReturn> saveQuestion(@FieldMap Map<String, String> map);

        //修改密码
        @FormUrlEncoded
        @POST("/jf/api/user/changePass")
        Observable<CommonReturn> changePass(@Field("passOld") String passOld, @Field("passNew") String passNew);

        //修改手机号
        @FormUrlEncoded
        @POST("/jf/api/user/changeMobile")
        Observable<CommonReturn> changeMobile(@Field("newMobile") String newMobile, @Field("valicode") String valicode);

        //找回密码
        @FormUrlEncoded
        @POST("/jf/api/user/findPass")
        Observable<CommonReturn> findPass(@Field("username") String username, @Field("valicode") String valicode, @Field("password") String password);

        //绑定的企业列表
        @POST("/jf/api/bind/index")
        Observable<BindReturn> bindIndex();

        //解除绑定
        @FormUrlEncoded
        @POST("/jf/api/bind/delete")
        Observable<CommonReturn> bindDelete(@Field("bindids") String bindids);

        //污染源列表
        @FormUrlEncoded
        @POST("/jf/api/enterprise/queryByOrg")
        Observable<EnterpriseReturn> queryByOrgEnterprise(@FieldMap Map<String, String> map);

        //污染源PopupWindow列表
        @FormUrlEncoded
        @POST("/jf/api/department/child")
        Observable<DepartmentChildReturn> departmentChild(@FieldMap Map<String, String> map);

        //系统通知列表
        @FormUrlEncoded
        @POST("/jf/api/notice/index")
        Observable<NoticeReturn> noticeIndex(@FieldMap Map<String, String> map);

        //咨询详情
        @POST("/jf/api/question/view/{id}")
        Observable<ZiXunDetailReturn> questionDetail(@Path("id") String id);

        //首页数据
        @POST("/jf/api/index/index")
        Observable<IndexIndexReturn> indexIndex();

        //工作数据
        @POST("/jf/api/work/index")
        Observable<WorkIndexReturn> workIndex();

        //体检
        @FormUrlEncoded
        @POST("/jf/api/index/errorList")
        Observable<ErrorListReturn> errorList(@FieldMap Map<String, String> map);

        //企业中心
        @POST("/jf/api/enterprise/view/{id}")
        Observable<EnterpriseViewReturn> enterpriseView(@Path("id") String id);

        //新闻详情
        @POST("/jf/api/news/view/{id}")
        Observable<NewsViewReturn> newsView(@Path("id") String id);

        //巡查数据列表
        @FormUrlEncoded
        @POST("/jf/api/inspectflow/index")
        Observable<InspectflowIndexReturn> inspectflowIndex(@FieldMap Map<String, String> map);

        //提交数据 巡查数据
        @FormUrlEncoded
        @POST("/jf/api/inspect/view")
        Observable<InspectViewReturn> inspectView(@FieldMap Map<String, String> map);

        //提交数据 巡查数据列表过来的接口
        @FormUrlEncoded
        @POST("/jf/api/inspect/view/{inspectid}")
        Observable<InspectViewReturn> inspectViewWithInspectid(@FieldMap Map<String, String> map, @Path("inspectid") String inspectid);

        //编辑资料 企业详情
        @POST("/jf/api/enterprise/detail/{id}")
        Observable<EnterpriseDetailReturn> enterpriseDetail(@Path("id") String id);

        //异常数据提交 列表
        @FormUrlEncoded
        @POST("/jf/api/report/index")
        Observable<ReportIndexReturn> reportIndex(@FieldMap Map<String, String> map);

        //异常数据提交 详情
        @POST("/jf/api/report/view/{id}")
        Observable<ReportViewReturn> reportView(@Path("id") String id);

        //异常数据提交 创建
        @FormUrlEncoded
        @POST("/jf/api/report/save")
        Observable<CommonReturn> reportSave(@FieldMap Map<String, String> map);

        //提交审核 关联企业
        @FormUrlEncoded
        @POST("/jf/api/enterprise/bind")
        Observable<CommonReturn> enterpriseBind(@FieldMap Map<String, String> map);

        //根据经纬度和距离请求污染源
        @FormUrlEncoded
        @POST("/jf/api/department/area")
        Observable<DepartmentAreaReturn> departmentArea(@FieldMap Map<String, String> map);

        //搜索污染源
        @FormUrlEncoded
        @POST("/jf/api/enterprise/query")
        Observable<EnterpriseQueryReturn> enterpriseQuery(@Field("name") String name);

        //地图区域范围
        @POST("/jf/api/departmentscope/index")
        Observable<DepartmentscopeIndexReturn> departmentscopeIndex();

        //更新企业资料
        @FormUrlEncoded
        @POST("/jf/api/enterprise/update/{id}")
        Observable<CommonReturn> enterpriseUpdate(@Path("id") String id, @FieldMap Map<String, String> map);

        //更新巡查数据
        @FormUrlEncoded
        @POST("/jf/api/inspect/update/{id}")
        Observable<CommonReturn> inspectUpdate(@Path("id") String id, @FieldMap Map<String, String> map);

        //意见反馈
        @FormUrlEncoded
        @POST("/jf/api/feedback/save")
        Observable<CommonReturn> feedbackSave(@FieldMap Map<String, String> map);

        //行业分类
        @POST("/jf/api/dict/categoryTreeData")
        Observable<CategoryTreeDataReturn> categoryTreeData();

        //巡查数据审核
        @FormUrlEncoded
        @POST("/jf/api/inspectflow/audit")
        Observable<CommonReturn> inspectflowAudit(@FieldMap Map<String, String> map);

        //获取最新版本信息
        @POST("/jf/api/version/android")
        Observable<UpDateInfo> versionAndroid();


    }

    public static Retrofit getRetrofit() {
        if (sRetrofit == null) {
            OkHttpClient httpClient = new OkHttpClient();
            httpClient.setConnectTimeout(20, TimeUnit.SECONDS);
            httpClient.setReadTimeout(20, TimeUnit.SECONDS);
            httpClient.setWriteTimeout(20, TimeUnit.SECONDS);
            httpClient.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            httpClient.interceptors().add(new TokenInterceptor());
            sRetrofit = new Retrofit.Builder()
                    .client(httpClient)
                    .baseUrl(API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return sRetrofit;
    }


}



